package online.inote.dic.common.utils.spring;

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import online.inote.dic.common.utils.StringUtils;

/**
 * <p>
 * Desc: Spring配置属性工厂类(要项目此类生效需要配置Spring扫描路径包含当前类的路径)
 * </p>
 * 
 * @author Sui
 * @created: 2019年2月12日 下午5:41:49
 * @version 1.0
 */
@Component
public class PropertyFactory implements EnvironmentAware {

	public static Environment env;
	public static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active";
	
	public enum Active {
		DEV, TEST, GRAY, PROD;
		
		public static Active getActive(String params) {
			
			for (Active active : values()) {
				if (StringUtils.equals(params.toUpperCase(), active.toString())) {
					return active;
				}
			}
			
			return PROD;
		}
	}
	
	@Override
	public void setEnvironment(Environment environment) {
		env = environment;
	}
	
	public static String getProperty(String key) {
		return env.getProperty(key);
	}
	
	public static String getProperty(String key, String defaultValue) {
		return env.getProperty(key, defaultValue);
	}
	
	public static Environment getEnvironment() {
		return env;
	}
	
	public static Active getActive() {
		return Active.getActive(getProperty(SPRING_PROFILES_ACTIVE));
	}
	
	public static void main(String[] args) {
		System.out.println(PropertyFactory.getActive());
	}
}